package com.meessiom.train.crm.role.controller;
import com.meessiom.train.crm.role.model.Role;
import com.meessiom.train.crm.role.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CrossOrigin("*") //是用来处理跨源资源共享
@RestController //控制器
@RequestMapping("/api/v1/roles") //这是控制器的地址
@Slf4j
public class RoleController {
    @Autowired////成员变量、方法和构造函数进行标注，来完成自动装配的工作
    private RoleService roleService;
    @GetMapping//用于处理请求方法
    public List<Role> getAllRole() {
        log.info("获得了来自客户端的获得学生列表的的请求");
        return roleService.getAllRoles();
    }

    @GetMapping({"{id}"})
    public ResponseEntity<Role> getRoleById(@PathVariable long id) {
        Role role = this.roleService.getRoleById(id);
        return ResponseEntity.ok(role);
    }

    @PutMapping({"{id}"})
    public ResponseEntity<Role> updateRole(@PathVariable long id, @RequestBody Role roleDetails) {
        Role updateRole = this.roleService.updateRole(id, roleDetails);
        return ResponseEntity.ok(updateRole);
    }


    @DeleteMapping({"{id}"})
    public ResponseEntity<HttpStatus> deleteRole(@PathVariable long id) {
        this.roleService.deleteRole(id);
        return new ResponseEntity(HttpStatus.NO_CONTENT);


    }
}
